package it.tcon.xbeedriver.resmanager;

import it.tcon.xbeedriver.XBeeNode;
import it.tcon.xbeedriver.XBeeNodeList;

import org.apache.log4j.Logger;


import com.rapplogic.xbee.api.zigbee.ZNetRemoteAtResponse;
import com.rapplogic.xbee.util.ByteUtils;

public class RemoteAtCommandManager {
	
	private XBeeNodeList list;
	private XBeeNode thisNode;
	private Logger logger = Logger.getLogger(RemoteAtCommandManager.class);
	
	
	public RemoteAtCommandManager(ZNetRemoteAtResponse response,XBeeNodeList list){
		this.list = list;
		logger.debug("address: " +ByteUtils.toBase16(response.getRemoteAddress64().getAddress()));
		String address = ByteUtils.toBase16(response.getRemoteAddress64().getAddress());
		this.thisNode = list.getNode(address);
		if(address != null && thisNode != null){
			responseCommandCheck(response);
		}
		else{
			//TODO if the node is not present
			logger.debug("The node "+address+"is not present");
		}
			
	}
	public void responseCommandCheck(ZNetRemoteAtResponse response){
		
		String command = response.getCommandName();
		int[] value = response.getCommandData();
		if(value == null){
			logger.debug("Value = null");
			return;
		}
		switch(response.getStatus().getValue()){
		case 0: // OK STATUS
			break;
		case 1: // ERROR 
			logger.debug("Error on command received");
			return;
		case 2: // INVALID_COMMAND
			logger.debug("Invalid command received");
			return;
		case 3: // INVALID_PARAMETER
			logger.debug("Invalid parameter received");
			return;
			
		}
		//logger.debug("Address: "+ response.)
		if(command.compareTo("DH") == 0){
			logger.debug("Response of an DH command");
			this.thisNode.setDestinationAddrHigh(value);
			this.list.updateNode(this.thisNode);
		}	
		else if(command.compareTo("DL") == 0){
			logger.debug("Response of an DL command");
			this.thisNode.setDestinationAddrLow(value);
			this.list.updateNode(this.thisNode);
			
		}
		else if(command.compareTo("MY") == 0){
			logger.debug("Response of an MY command");
			this.thisNode.setMy16BitAddress(value);
			this.list.updateNode(this.thisNode);
			
		}
		else if(command.compareTo("MP") == 0){
			/*logger.debug("Response of an MP command");
			this.thisNode.setM(value);
			this.list.updateNode(this.thisNode);*/
		}
		else if(command.compareTo("SH") == 0){
			
			logger.debug("Response of an SH command");
			this.thisNode.setSerialNumberHigh(value);
			this.list.updateNode(this.thisNode);
			
		}
		else if(command.compareTo("SL") == 0){
			logger.debug("Response of an SL command");
			this.thisNode.setSerialNumberLow(value);
			this.list.updateNode(this.thisNode);
			
		}
		else if(command.compareTo("NI") == 0){
			logger.debug("Response of an NI command");
			this.thisNode.setNodeIdentifier(value);
			this.list.updateNode(this.thisNode);
		}
		else if(command.compareTo("DD") == 0){
			logger.debug("Response of an DD command");
			this.thisNode.setDeviceTypeIdentifier(value);
			this.list.updateNode(this.thisNode);
		}
		else if(command.compareTo("ZA") == 0){
			
		}
		else if(command.compareTo("SE") == 0){
			
		}
		else if(command.compareTo("DE") == 0){
			
		}
		else if(command.compareTo("CI") == 0){
			
		}
		else if(command.compareTo("BI") == 0){
			
		}
		else if(command.compareTo("CH") == 0){
			logger.debug("Response of an CH command");
			this.thisNode.setOperatingChannel(value);
			this.list.updateNode(this.thisNode);
		}
		else if(command.compareTo("ID") == 0){
			logger.debug("Response of an ID command");
			this.thisNode.setPANID(value);
			this.list.updateNode(this.thisNode);
		}
		else if(command.compareTo("BH") == 0){
			logger.debug("Response of an BH command");
			this.thisNode.setBroadcastRadius(value);
			this.list.updateNode(this.thisNode);
		}
		//TODO Check if it's necessary to implement an ND discover to the Router or End-Device
		/*
		else if(command.compareTo("ND") == 0){
			NodeDiscover discoverNode = null;
			discoverNode = NodeDiscover.parse(response);
			if(discoverNode != null){
				XBeeNode newNode = new XBeeNode();
				//newNode.setAddress(discoverNode.getNodeAddress64());
				//newNode.setAddress16bit(discoverNode.getNodeAddress16());
				newNode.setAddress64bit(discoverNode.getNodeAddress64());
				newNode.setType(new XBeeNodeType(discoverNode.getDeviceType().getValue()));
				
				try {
					this.list.addNode(newNode.getAddress64Bit(),newNode);
				} catch (XBeeNodeException e) {
						System.out.println("Error on adding a module");
						logger.error("Error adding an XBee module");
					e.printStackTrace();
				}
				logger.debug("Added a XBee Module");

				System.out.println("Module added");
				//if(addDiscoverDevice(newNode)){
				//	System.out.println("Device added");
				//}
			}
			
		}*/
		else if(command.compareTo("DN") == 0){
			
		}
		else if(command.compareTo("JN") == 0){
			
		}
		else if(command.compareTo("SC") == 0){
			logger.debug("Response of an SC command");
			this.thisNode.setScanChannels(value);
			this.list.updateNode(this.thisNode);
		}
		else if(command.compareTo("SD") == 0){
			logger.debug("Response of an SD command");
			this.thisNode.setScanDuration(value);
			this.list.updateNode(this.thisNode);
		}
		else if(command.compareTo("NJ") == 0){
			logger.debug("Response of an NJ command");
			this.thisNode.setNodeJoinDuration(value);
			this.list.updateNode(this.thisNode);
		}
		else if(command.compareTo("AR") == 0){
			logger.debug("Response of an AR command");
			this.thisNode.setAggregationRouteBCTime(value);
			this.list.updateNode(this.thisNode);
		}
		else if(command.compareTo("AI") == 0){
			
		}
		else if(command.compareTo("PL") == 0){ // Power Level
			logger.debug("Response of an PL command");
			this.thisNode.setPowerLevel(value);
			this.list.updateNode(this.thisNode);
		}
		else if(command.compareTo("PM") == 0){ // Power Mode
			logger.debug("Response of an PM command");
			this.thisNode.setPowerMode(value);
			this.list.updateNode(this.thisNode);
		}
		else if(command.compareTo("AP") == 0){
			logger.debug("Response of an AP command");
			this.thisNode.setAPIEnable(value);
			this.list.updateNode(this.thisNode);
		}
		else if(command.compareTo("AO") == 0){
			logger.debug("Response of an AO command");
			this.thisNode.setAPIOptions(value);
			this.list.updateNode(this.thisNode);
		}
		else if(command.compareTo("BD") == 0){
			logger.debug("Response of an BD command");
			this.thisNode.setBaudRate(value);
			this.list.updateNode(this.thisNode);
		}
		else if(command.compareTo("VR") == 0){
			logger.debug("Response of an VR command");
			this.thisNode.setFirmwareVersion(value);
			this.list.updateNode(this.thisNode);
			
		}
		else if(command.compareTo("HV") == 0){
			logger.debug("Response of an HV command");
			this.thisNode.setHardwareVersion(value);
			this.list.updateNode(this.thisNode);
		}
		
	}
}
